package com.power.home.ui.activity;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.power.home.GSYVideoPlayer.LandLayoutVideo;
import com.power.home.R;
import com.power.home.common.Constant;
import com.power.home.common.bus.BindEventBus;
import com.power.home.common.bus.EventBusEvent;
import com.power.home.common.bus.EventBusUtils;
import com.power.home.common.util.FloatMusicUtil;
import com.power.home.common.util.FontUtil;
import com.power.home.common.util.MusicList;
import com.power.home.common.util.SharePreferencesUtils;
import com.power.home.common.util.StatusBarUtil;
import com.power.home.common.util.StringUtil;
import com.power.home.common.util.ToastUtils;
import com.power.home.common.util.UIUtil;
import com.power.home.common.util.UserCacheUtil;
import com.power.home.data.bean.CommodityBean;
import com.power.home.data.bean.CoursePlayerBean;
import com.power.home.data.bean.Music;
import com.power.home.data.bean.ShareBean;
import com.power.home.di.component.AppComponent;
import com.power.home.di.component.DaggerCoursePlayerComponent;
import com.power.home.di.module.CoursePlayerModule;
import com.power.home.jzvd.JZDataSource;
import com.power.home.jzvd.JZUtils;
import com.power.home.jzvd.Jzvd;
import com.power.home.jzvd.JzvdStd;
import com.power.home.presenter.CoursePlayerPresenter;
import com.power.home.presenter.contract.CoursePlayerContract;
import com.power.home.ui.fragment.CourseDetailsCatalogueFragment;
import com.power.home.ui.fragment.CourseDetailsIntroductionFragment;
import com.power.home.ui.service.MediaService;
import com.power.home.ui.widget.MyJzPlayer;
import com.power.home.ui.widget.MySharePopup;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;

import static android.view.View.VISIBLE;
import static com.power.home.jzvd.Jzvd.STATE_PLAYING;
import static com.power.home.jzvd.Jzvd.WIFI_TIP_DIALOG_SHOWED;

@BindEventBus
@Route(path = "/android/video")
public class CourseGSYPlayerActivity  extends BaseActivity<CoursePlayerPresenter> implements CoursePlayerContract.View, View.OnClickListener{
//    @BindView(R.id.mJC)
//    MyJzPlayer myJzvdStd;


//    Jzvd.JZAutoFullscreenListener mSensorEventListener;
//    SensorManager mSensorManager;//重力感应
    @BindView(R.id.tv_course_title)
    TextView tvCourseTitle;
    @BindView(R.id.tv_share)
    TextView tvShare;
    @BindView(R.id.tv_course_subtitle)
    TextView tvCourseSubtitle;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.tv_album_buy)
    TextView tvAlbumBuy;
    @BindView(R.id.ll_album_buy)
    LinearLayout llAlbumBuy;
    @BindView(R.id.ll_bottom_buy)
    LinearLayout ll_bottom_buy;
    @BindView(R.id.tv_growth_buy)
    TextView tvGrowthBuy;
    @BindView(R.id.tv_album_state)
    TextView tvAlbumState;
    @BindView(R.id.tv_toGroth)
    TextView tvToGroth;
    @BindView(R.id.tv_receive)
    TextView tvReceive;
    @BindView(R.id.tv_course_size)
    TextView tv_course_size;
    @BindView(R.id.viwePagerContainer)
    LinearLayout viwePagerContainer;
    @BindView(R.id.detail_player)
    LandLayoutVideo detailPlayer;

   CoursePlayerBean.CoursesBean currentBean;
    private String id;//课程或者专辑id
    private String type;//课程或者专辑

    private Jzvd.AlbumOrientationEventListener albumOrientationEventListener;
    private ArrayList<Fragment> mFragmentList;
    private CourseDetailsCatalogueFragment catalogueFragment;//课程选集
    private CourseDetailsIntroductionFragment introductionFragment;//课程简介
    private CoursePlayerBean coursePlayerBean;
    private int position;//视频列表脚本
    private String coursesId;
    private String coruseTitle;
    private String courseSubtitle;
    private Sensor accelerometerSensor;
    private boolean isClickAudio = false;
    private String shareIcon;
    private String courseDisplayPic;
    private int seek;
    private Music fromMusic;
    private boolean canPlay;
    private Animation rotateAnimation;
    private boolean isPlay;
    private String from;
    private Music currentMusic;//进入此页面的音频文件
    private OrientationUtils orientationUtils;
    @Override
    public int setLayout() {
        setStatusBarColor(this, getResources().getColor(R.color.colorBlack));
        return R.layout.activity_coursenewplayer;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent != null){
            id = intent.getStringExtra(Constant.id);
            type = intent.getStringExtra(Constant.type);
            from = intent.getStringExtra(Constant.from);
            fromMusic = (Music)intent.getSerializableExtra(Constant.seek);
            if (StringUtil.isNullString(from) && null != MediaService.mMediaPlayer) {//正常进入 可能播放 也可能没在播放音频 全都暂停 打开的不是点击浮窗的资源，默认播放视频,暂停掉上次的音频
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_STOP, EventBusUtils.EventCode.SERVICE_STOP));
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE, EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE));
            }
        }
    }

    @Override
    public void getExtras() {
        id = getIntent().getStringExtra(Constant.id);
        type = getIntent().getStringExtra(Constant.type);
        from = getIntent().getStringExtra(Constant.from);
        fromMusic = (Music) getIntent().getSerializableExtra(Constant.seek);//浮窗进入携带数据

        if (StringUtil.isNullString(from) && null != MediaService.mMediaPlayer) {//正常进入 可能播放 也可能没在播放音频 全都暂停 打开的不是点击浮窗的资源，默认播放视频,暂停掉上次的音频
//            EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_STOP, EventBusUtils.EventCode.SERVICE_STOP));
//            EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE, EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE));
        }
    }

    @Override
    public boolean init() {
        initView();
        initTabLayout();
        mPresenter.getCourseDetails(type, id);

        configPlayer();

        return false;

    }

    private void initView() {



    }

    private void configPlayer(){


        orientationUtils = new OrientationUtils(this, detailPlayer);


        //初始化不打开外部的旋转
        orientationUtils.setEnable(false);
        detailPlayer.setNeedShowWifiTip(true);

        detailPlayer.setRotateViewAuto(false);
        detailPlayer.setLockLand(false);
        detailPlayer.setShowFullAnimation(false);
        detailPlayer.setNeedLockFull(true);
//
        detailPlayer.setRotateWithSystem(false);
        detailPlayer.setSeekRatio(5);

        detailPlayer.setMapHeadData(new HashMap<String, String>());




//
        detailPlayer.setVideoAllCallBack(new GSYSampleCallBack(){
            @Override
            public void onPrepared(String url, Object... objects) {
                super.onPrepared(url, objects);
                orientationUtils.setEnable(detailPlayer.isRotateWithSystem());
            }

            @Override
            public void onAutoComplete(String url, Object... objects) {
                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.VIDEO_COMPLETE,"" ));
                super.onAutoComplete(url, objects);
            }

            @Override
            public void onEnterFullscreen(String url, Object... objects) {
                super.onEnterFullscreen(url, objects);

            }

            @Override
            public void onQuitFullscreen(String url, Object... objects) {
                super.onQuitFullscreen(url, objects);
                if (orientationUtils != null) {
                    orientationUtils.backToProtVideo();
                }

            }

            @Override
            public void onTouchScreenSeekPosition(String url, Object... objects) {
                super.onTouchScreenSeekPosition(url, objects);
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_SEEK, detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying()));
            }
        });
        detailPlayer.setLockClickListener(new LockClickListener() {
            @Override
            public void onClick(View view, boolean lock) {

                    orientationUtils.setEnable(!lock);

            }
        });

        detailPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();


            }
        });

        detailPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    orientationUtils.resolveByClick();
                    detailPlayer.startWindowFullscreen(CourseGSYPlayerActivity.this, true, true);





            }
        });

    }



    private void initPlayer(CoursePlayerBean.CoursesBean coursesBean){
        coursesId = coursesBean.getId();//当前播放课程id
        coruseTitle = coursesBean.getTitle();
        courseSubtitle = coursesBean.getSubtitle();
        courseDisplayPic = coursesBean.getDisplayPic();
        shareIcon = coursesBean.getShareIcon();

        canPlay = coursesBean.isCanPlay();
        String[] videoUrls = coursesBean.getVideoUrl().split(";");


        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.icon_place_holder_375_211)
                .error(R.drawable.icon_place_holder_375_211)
                .fallback(R.drawable.icon_place_holder_375_211);






        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(this).load(coursesBean.getCourseCover()).apply(requestOptions).into(imageView);

        detailPlayer.getCurrentPlayer().setThumbImageView(imageView);


//        detailPlayer.playerNext(encodeUrl);


        detailPlayer.getCurrentPlayer().setUp(videoUrls[0],true,null);
        currentBean = coursesBean;






        playVideo(coursesBean.isFree(), coursesBean.isCanPlay());
    }

    private void seekToTime(){

        if (seek > 0) {//外部悬浮携带进度进入

            detailPlayer.getCurrentPlayer().setSeekOnStart(seek);

        } else {
            detailPlayer.getCurrentPlayer().setSeekOnStart(currentBean.getLatestProgress() * 1000);
        }
        Log.i("前进-----",String.valueOf(seek*1000));
    }
    private void changeViewPagerContanerMarginBottom(int bottom){
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) viwePagerContainer.getLayoutParams();

        p.bottomMargin = UIUtil.dip2px(bottom);
        viwePagerContainer.setLayoutParams(p);
    }
    /**
     * 播放视频
     */
    private void playVideo(boolean isFree, boolean isCanPlay){
        if(isFree){

            detailPlayer.my_audio_video_change.setVisibility(VISIBLE);
            detailPlayer.rl_buy_album.setVisibility(View.GONE);


            if(isClickAudio){

            }else{
                this.seekToTime();
                detailPlayer.getCurrentPlayer().startPlayLogic();

//                if(!JZUtils.isWifiConnected(this) && !WIFI_TIP_DIALOG_SHOWED){
////                    myJzvdStd.showWifiDialog();
//
//                    return;
//                }else{
////                    myJzvdStd.startVideo();
//                    detailPlayer.startPlayLogic();
//                }
            }

        }else{

            if(isCanPlay){
                detailPlayer.my_audio_video_change.setVisibility(View.VISIBLE);
                detailPlayer.rl_buy_album.setVisibility(View.GONE);
//                this.changeViewPagerContanerMarginBottom(0);
                if(isClickAudio){

                }else{
                    this.seekToTime();
                    detailPlayer.getCurrentPlayer().startPlayLogic();

                    //视频
//                    if (!JZUtils.isWifiConnected(this) && !WIFI_TIP_DIALOG_SHOWED) {
////                        myJzvdStd.showWifiDialog();
//                        return;
//                    } else {
//                        detailPlayer.startPlayLogic();
//                    }
                }
            }else{
                detailPlayer.my_audio_video_change.setVisibility(View.GONE);
                detailPlayer.rl_buy_album.setVisibility(VISIBLE);
//
                detailPlayer.tv_album_price.setText("¥" + coursePlayerBean.getPrice() + "/年 购买本专辑");
                detailPlayer.tv_album_hint.setText("您尚未购买本专辑，当前课程无法观看");
                if(detailPlayer.isIfCurrentIsFullscreen()){
                    detailPlayer.onBackFullscreen();
                }
//
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusEvent date){
        if (date != null) {
            switch (date.getCode()) {
                case EventBusUtils.EventCode.VIDEO_BEAN:
                    if (UserCacheUtil.isLogin()) {
                        mPresenter.setStudyProgress(coursesId, false, detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying()/1000 + "");
                    }
                    seek = 0;

                    CoursePlayerBean.CoursesBean clickBean = (CoursePlayerBean.CoursesBean) date.getData();
                    if(currentBean == clickBean){
                        return;
                    }
                    currentBean = clickBean;
                    position = clickBean.getPosition();

                    prepareNextMusic();
                    initPlayer(clickBean);

                    break;

                case EventBusUtils.EventCode.VIDEO_COMPLETE://切换下一节课

                    if (UserCacheUtil.isLogin()) {
                        mPresenter.setStudyProgress(coursesId, true, "0");
                    }
                    seek = 0;
//                    Jzvd.releaseAllVideos();
                    if (coursePlayerBean != null) {
                        if (position + 1 == coursePlayerBean.getCourses().size()) {
                            position = 0;
                        } else {
                            position++;
                        }
                        catalogueFragment.catalogueAdapter.setSelectPosition(position);
//                        myJzvdStd.antholigyAdapter.setSelectPosition(position);
                        CoursePlayerBean.CoursesBean coursesNextBean = coursePlayerBean.getCourses().get(position);
                        prepareNextMusic();
                        initPlayer(coursesNextBean);
                    }
                    break;
                case EventBusUtils.EventCode.LOGIN_BUY_REFRESH:
                    mPresenter.getCourseDetails(type, id);
                    type = "1";
                    break;
                case EventBusUtils.EventCode.APP_REFREASH_FLOAT_AUDIO_SECOND:
                    Music musicSecond = (Music) date.getData();
//
                    break;
                case EventBusUtils.EventCode.SERVICE_FLOAT_AUDIO_PLAY_PAUSE:
                    Music music = (Music) date.getData();
                    seek = music.getCurrentPosition();
                    if (isPlay) {
                        //正在播放 ---->暂停---->展示播放键
                        isPlay = false;
//
                        currentMusic.setPlaying(false);
                        if (null != MediaService.mMediaPlayer) {
                            seek = MediaService.mMediaPlayer.getCurrentPosition();
                        }
                    } else {
                        isPlay = true;
//
                        currentMusic.setPlaying(true);
                    }
                    break;
            }
        }
    }

    /**
     * 切换下一集 或者 准备下一集
     */
    private void prepareNextMusic() {
        currentMusic = MusicList.getMusic(position);

        currentMusic.setAudio(isClickAudio);
        if (isClickAudio) {
//            EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
            if (StringUtil.isNotNullString(currentMusic.getUrl()) && currentMusic.isCanPlay()) {
                //默认点击下一集 自动播放 更换按钮状态
                currentMusic.setPlaying(true);
//
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NEXT, currentMusic));
            } else {
                currentMusic.setPlaying(false);
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
                //点击不支持音频 或者 点击之前是视频   需要切换回视频页面
                //展示视频页面
                changeToVideo();
            }
        } else {
            if (StringUtil.isNotNullString(currentMusic.getUrl()) && currentMusic.isCanPlay()) {
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NEXT_PREPARE, currentMusic));
            } else {
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
                //点击不支持音频 或者 点击之前是视频   需要切换回视频页面
                //展示视频页面
                changeToVideo();
            }

        }
    }
    /*
     * 简介  目录  互动 Tab
     */
    private void initTabLayout() {

        String[] tabTips = {"专辑简介", "课程选集"};
        mFragmentList = new ArrayList<>();
        introductionFragment = new CourseDetailsIntroductionFragment();
        mFragmentList.add(introductionFragment);
        catalogueFragment = new CourseDetailsCatalogueFragment();
        mFragmentList.add(catalogueFragment);

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentList.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabTips[position];
            }
        });
        viewPager.setCurrentItem(1);
        tablayout.setupWithViewPager(viewPager);

    }
    @Override
    public void setListener() {
        StatusBarUtil.setBarDarkMode(this, false);
//        myJzvdStd.mIvShare.setOnClickListener(this);
        detailPlayer.tv_album_price.setOnClickListener(this);
//        myJzvdStd.buyShare.setOnClickListener(this);
        tvGrowthBuy.setOnClickListener(this);
        llAlbumBuy.setOnClickListener(this);
        tvShare.setOnClickListener(this);
        detailPlayer.my_audio_video_change.setOnClickListener(this);
        tvToGroth.setOnClickListener(this);
        tvReceive.setOnClickListener(this);

//        myJzvdStd.ivAudioStart.setOnClickListener(this);
//        myJzvdStd.ivAudioBack.setOnClickListener(this);
//        myJzvdStd.ivAudioShare.setOnClickListener(this);
//        myJzvdStd.seekProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//                myJzvdStd.tvAudioCurrent.setText(JZUtils.stringForTime(seekBar.getProgress()));
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_SEEK, seekBar.getProgress()));
//            }
//        });

    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {
        DaggerCoursePlayerComponent.builder().appComponent(appComponent)
                .coursePlayerModule(new CoursePlayerModule(this))
                .build().inject(this);
    }




    @Override
    protected void onResume() {
        super.onResume();

        if (!isClickAudio) {


            detailPlayer.getCurrentPlayer().onVideoResume();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_audio_back:
                onBackPressed();
                break;
            case R.id.iv_share:
            case R.id.iv_course_share:
            case R.id.tv_share:
            case R.id.iv_audio_share:

                if (UserCacheUtil.isLogin()) {
                    if (coursePlayerBean == null) {
                        ToastUtils.shortShow("网络异常,无法分享");
                        return;
                    }

                    if(detailPlayer.isIfCurrentIsFullscreen()){
                        detailPlayer.onBackFullscreen();
                    }

                    ShareBean shareBean = new ShareBean();
                    shareBean.setPosterImgs(coursePlayerBean.getPosterImgs());
                    shareBean.setSlogans(coursePlayerBean.getSlogans());
                    shareBean.setCourseName("《" + coursePlayerBean.getCourses().get(position).getTitle() + "》");
                    shareBean.setInviteFrontName("邀请您观看");
                    shareBean.setInviteBehindName("");
                    if (StringUtil.isEmpty(shareIcon)) {
                        shareBean.setShareImg(courseDisplayPic);
                    } else {
                        shareBean.setShareImg(shareIcon);
                    }

                    shareBean.setShareTitle(coruseTitle);
                    shareBean.setShareSubtitle(courseSubtitle);

                    String shareUrl = coursePlayerBean.getShareUrl();
                    if (StringUtil.isNullString(shareUrl)) {
                        return;
                    }
                    shareBean.setShareUrl(shareUrl.contains("?") ? shareUrl + "&courseId=" + coursesId : shareUrl + "?courseId=" + coursesId);

                    MySharePopup mySharePopup = new MySharePopup(this, shareBean);
                    mySharePopup.showPopWindow();
                } else {
                    goLogin();
                }

                break;
            case R.id.tv_album_price:
                if (coursePlayerBean == null) {
                    ToastUtils.shortShow("网络异常,无法购买");
                    return;
                }
                buyAlbum();
            case R.id.ll_album_buy:
                if (coursePlayerBean == null) {
                    ToastUtils.shortShow("网络异常,无法购买");
                    return;
                }
                if (coursePlayerBean.isBuy()) {
//                    ARouter.getInstance().build("/android/paid").navigation();
                } else {
                    buyAlbum();
                }
                break;
            case R.id.tv_growth_buy:
                if (coursePlayerBean == null) {
                    ToastUtils.shortShow("网络异常,无法购买");
                    return;
                }
                if (UserCacheUtil.isLogin()) {
                    ARouter.getInstance().build("/android/vipCenter").navigation();
                } else {
                    goLogin();
                }

                break;
            case R.id.tv_toGroth:
                if (UserCacheUtil.isLogin()) {
                    ARouter.getInstance().build("/android/vipCenter").navigation();
                } else {
                    goLogin();
                }
                break;
            case R.id.tv_receive:
                if (UserCacheUtil.isLogin()) {
                    if (coursePlayerBean == null) {
                        ToastUtils.shortShow("网络异常");
                        return;
                    }
                    Intent intent1 = new Intent(this, ReceiveMaterialActivity.class);
                    intent1.putExtra("displayPic", coursePlayerBean.getDisplayPic());
                    intent1.putExtra("courseTopicTitle", coursePlayerBean.getCourseTopicTitle());
                    intent1.putExtra("courseTopicSubtitle", coursePlayerBean.getCourseTopicSubtitle());
                    intent1.putExtra("descriptionUrl", coursePlayerBean.getDescriptionUrl());
                    startActivity(intent1);
                } else {
                    goLogin();
                }

                break;
            case R.id.my_audio_video_change:
                if (detailPlayer.my_audio_video_change.getText().toString().equals("音频")){
                    if(currentMusic == null || StringUtil.isNullString(currentMusic.getUrl())){
                        UIUtil.showToastSafe("本课程暂不支持音频");
                    }else{
                        isClickAudio = true;
                        EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_OPEN, EventBusUtils.EventCode.SERVICE_NOTIFICATION_OPEN));


                        isPlay = !(detailPlayer.getCurrentPlayer().getCurrentState()== GSYVideoView.CURRENT_STATE_PLAYING);
                        detailPlayer.getCurrentPlayer().onVideoPause();
                        seek = detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying();
                        detailPlayer.my_audio_video_change.setText("视频");
                        currentMusic.setAudio(true);
                        currentMusic.setPlaying(detailPlayer.getCurrentPlayer().getCurrentState() == GSYVideoView.CURRENT_STATE_PLAYING);
                        currentMusic.setDuration((int) detailPlayer.getCurrentPlayer().getDuration());
                        currentMusic.setCurrentPosition((int) detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying());
                        showAudioPlayOrPause();

                    }
                }else{
                    EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
                    showVideoPage();
                }
//                if (myJzvdStd.my_audio_video_change.getText().toString().equals("音频")) {
//                    if (null == currentMusic || StringUtil.isNullString(currentMusic.getUrl())) {
//                        UIUtil.showToastSafe("本课程暂不支持音频");
//                    } else {
//                        isClickAudio = true;
//
//                        isPlay = !(myJzvdStd.state == STATE_PLAYING);//如果播放中 更改isPlay  调用showAudioPlayOrPause方法去更改isPlay的值
//                        Jzvd.goOnPlayOnPause();//视频暂停播放
//                        if (myJzvdStd.screen == myJzvdStd.SCREEN_FULLSCREEN) {
//                            myJzvdStd.gotoScreenNormal();
//                        }
//                        myJzvdStd.rlAudioBg.setVisibility(VISIBLE);
//                        myJzvdStd.llAudioController.setVisibility(VISIBLE);
//                        seek = (int) myJzvdStd.getCurrentPositionWhenPlaying();
//                        myJzvdStd.tvAudioCurrent.setText(JZUtils.stringForTime(seek));
//                        myJzvdStd.tvAudioTotal.setText(JZUtils.stringForTime(currentMusic.getDuration()));
//                        myJzvdStd.my_audio_video_change.setText("视频");
//                        mSensorManager.unregisterListener(mSensorEventListener);
//                        currentMusic.setAudio(true);
//                        currentMusic.setPlaying(myJzvdStd.state == STATE_PLAYING);
//                        currentMusic.setDuration((int) myJzvdStd.getDuration());
//                        currentMusic.setCurrentPosition((int) myJzvdStd.getCurrentPositionWhenPlaying());
//                        showAudioPlayOrPause();
//                    }
//                } else {
//                    EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
//                    showVideoPage();
//                }
                break;
            case R.id.iv_audio_start:
                showAudioPlayOrPause();
                break;
        }
    }

    @Override
    public void getCourseDetailsSuces(CoursePlayerBean coursePlayerBean) {
        this.coursePlayerBean = coursePlayerBean;
        detailPlayer.my_audio_video_change.setVisibility(VISIBLE);//涉及音频进入切换问题，数据成功以后再显示

        catalogueFragment.catalogueAdapter.setNewData(coursePlayerBean.getCourses());
//        myJzvdStd.antholigyAdapter.setNewData(coursePlayerBean.getCourses());
        String descriptionUrl = coursePlayerBean.getDescriptionUrl();
        Boolean isContainUrl = StringUtil.isContainUrl(descriptionUrl);
        if(isContainUrl){
            String urlString = StringUtil.getUrl(descriptionUrl);
            introductionFragment.webView.loadUrl(urlString);
        }else{
            String htmlString = StringUtil.appendHtmlString(descriptionUrl);
            introductionFragment.webView.loadDataWithBaseURL(null, htmlString,"text/html;charset=utf-8","utf-8",null);
        }
        introductionFragment.webView.loadUrl(descriptionUrl);
        if (null != fromMusic) {//悬浮窗进入
            seek = fromMusic.getCurrentPosition();
        }
        if (!StringUtil.isEmptyList(coursePlayerBean.getCourses())) {
            if (StringUtil.isEquals(type, "2")) {
                for (int i = 0; i < coursePlayerBean.getCourses().size(); i++) {
                    if (StringUtil.isEquals(id, coursePlayerBean.getCourses().get(i).getId())) {
                        position = i;
                        catalogueFragment.catalogueAdapter.setSelectPosition(i);
//                        myJzvdStd.antholigyAdapter.setSelectPosition(i);
                        initPlayer(coursePlayerBean.getCourses().get(i));
                        break;
                    }
                }
            } else {
                catalogueFragment.catalogueAdapter.setSelectPosition(position);
                catalogueFragment.catalogueAdapter.setSelectPosition(position);
                initPlayer(coursePlayerBean.getCourses().get(position));
            }
        }
        initService();
        FontUtil.setFont(tvAlbumBuy);
        tvCourseTitle.setText(coursePlayerBean.getCourseTopicTitle());
        tvCourseSubtitle.setText(coursePlayerBean.getCourseTopicSubtitle());
        if (coursePlayerBean.isContinueUpdating()) {
            tv_course_size.setText("更新至" + coursePlayerBean.getCourseSize() + "节/共" + coursePlayerBean.getCourseTotalSize() + "节");
        } else {
            tv_course_size.setText("已完结/共" + coursePlayerBean.getCourseTotalSize() + "节");
        }
        ll_bottom_buy.setVisibility(VISIBLE);
        this.changeViewPagerContanerMarginBottom(48);
        if (StringUtil.isEmpty(coursePlayerBean.getBelongsTo())) {
            if (coursePlayerBean.isBuy()) {
                //专辑已购买
                llAlbumBuy.setVisibility(View.VISIBLE);
                tvAlbumBuy.setVisibility(View.GONE);
                tvGrowthBuy.setVisibility(View.GONE);
                tvAlbumState.setText("专辑已购买");
//                tvGrowthBuy.setText("加入VIP免费学");
                tvAlbumState.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            } else {
                llAlbumBuy.setVisibility(VISIBLE);
                tvAlbumBuy.setVisibility(View.GONE);
                tvGrowthBuy.setVisibility(View.GONE);
                tvAlbumState.setText("购买专辑 ¥" + coursePlayerBean.getPrice() + "/年");
//                tvGrowthBuy.setText("加入VIP免费学");
                llAlbumBuy.setBackgroundColor(getResources().getColor(R.color.colorBlue0D7EF9));
                tvAlbumState.setTextColor(getResources().getColor(R.color.colorWhite));
                tvAlbumState.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            }
        } else {
            if (coursePlayerBean.isVip()) {
                //VIP已购买
                llAlbumBuy.setVisibility(View.GONE);
                tvGrowthBuy.setText("续费VIP");
            } else {
                //VIP未购买
                if (coursePlayerBean.isBuy()) {
                    //专辑已购买
                    llAlbumBuy.setVisibility(VISIBLE);
                    tvAlbumBuy.setVisibility(View.GONE);
                    tvAlbumState.setText("专辑已购买");
                    tvGrowthBuy.setText("加入VIP免费学");
                    tvAlbumState.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                } else {
                    llAlbumBuy.setVisibility(VISIBLE);
                    tvAlbumBuy.setVisibility(VISIBLE);
                    tvAlbumState.setText("购买专辑");
                    tvAlbumBuy.setText(" ¥" + coursePlayerBean.getPrice() + "/年");
                    tvGrowthBuy.setText("加入VIP免费学");
                }
            }
        }

        if (Double.parseDouble(coursePlayerBean.getPrice()) == 0) {
            llAlbumBuy.setVisibility(View.GONE);
            ll_bottom_buy.setVisibility(View.GONE);
            this.changeViewPagerContanerMarginBottom(0);
//                tvGrowthBuy.setText("加入VIP免费学");
        }
    }
    private void showFromAudioPage() {
        //因为音频进入 默认直接播放
//        EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_OPEN, EventBusUtils.EventCode.SERVICE_NOTIFICATION_OPEN));
        isClickAudio = true;
//        myJzvdStd.rlAudioBg.setVisibility(VISIBLE);
//        myJzvdStd.llAudioController.setVisibility(VISIBLE);
//        mSensorManager.unregisterListener(mSensorEventListener);
        detailPlayer.my_audio_video_change.setText("视频");
        showAudioPlayOrPause();
        if (isPlay) {
            //正在播放 ---->暂停---->展示播放键
            isPlay = false;
//            myJzvdStd.civHead.clearAnimation();
//            myJzvdStd.ivAudioStart.setImageDrawable(UIUtil.getDrawable(R.drawable.jz_click_play_selector));
            currentMusic.setPlaying(false);
            if (null != MediaService.mMediaPlayer) {
                seek = MediaService.mMediaPlayer.getCurrentPosition();
            }
        } else {
            isPlay = true;

            currentMusic.setPlaying(true);
        }
    }
    private void changeToVideo() {
        isClickAudio = false;
//        EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE, EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE));
            detailPlayer.my_audio_video_change.setText("音频");

                detailPlayer.my_audio_video_change.setText("音频");

    }
    private void showVideoPage() {
        changeToVideo();
        seek = MediaService.mMediaPlayer.getCurrentPosition();
        currentMusic.setAudio(false);
        detailPlayer.getCurrentPlayer().setSeekOnStart(seek);
        detailPlayer.getCurrentPlayer().startPlayLogic();


        if (isPlay) {
//            Jzvd.goOnPlayOnResume();
            detailPlayer.getCurrentPlayer().onVideoResume();
        } else {
//            detailPlayer.getCurrentPlayer().startPlayLogic();
            detailPlayer.getCurrentPlayer().onVideoResume(true);

        }
    }
    private void showAudioPlayOrPause() {
        if (isPlay) {
            //正在播放 ---->暂停---->展示播放键
            isPlay = false;

            currentMusic.setPlaying(false);
//            EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PAUSE, currentMusic));
            if (null != MediaService.mMediaPlayer) {
                seek = MediaService.mMediaPlayer.getCurrentPosition();
            }
        } else {
            isPlay = true;

            currentMusic.setPlaying(true);
//            EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_PLAY, currentMusic));
        }
    }
    private void bugGroth() {
        if (UserCacheUtil.isLogin()) {
            CommodityBean commodityBean = new CommodityBean();
            commodityBean.setImg(coursePlayerBean.getGrowingIcon());
            commodityBean.setPrice(coursePlayerBean.getGrowingPrice());
            commodityBean.setTitle("VIP");
            commodityBean.setTopicId("0");
            commodityBean.setType("2");
            Intent intent2 = new Intent(this, ChosePayWayActivity.class);
            intent2.putExtra(Constant.commodity, commodityBean);
            startActivity(intent2);
            SharePreferencesUtils.saveProductName("你已成功购买VIP");
        } else {
            goLogin();
        }
    }

    private void buyAlbum() {
        if (UserCacheUtil.isLogin()) {
            CommodityBean commodityBean = new CommodityBean();
            commodityBean.setPrice(coursePlayerBean.getPrice());
            commodityBean.setTopicId(coursePlayerBean.getTopicId());
            commodityBean.setType(coursePlayerBean.getClassification());
            commodityBean.setImg(coursePlayerBean.getDisplayPic());
            commodityBean.setTitle(coursePlayerBean.getCourseTopicTitle());
            commodityBean.setSubTitle(coursePlayerBean.getCourseTopicSubtitle());
            SharePreferencesUtils.saveProductName("你已成功购买《" + coursePlayerBean.getCourseTopicTitle() + "》课程，赶紧去学习吧！");

            Intent intent1 = new Intent(this, ChosePayWayActivity.class);
            intent1.putExtra(Constant.commodity, commodityBean);
            startActivity(intent1);

        } else {
            goLogin();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
//

        JZUtils.clearSavedProgress(this, null);
        //home back

        detailPlayer.getCurrentPlayer().onVideoPause();
    }

    @Override
    protected void onDestroy() {
        if (isClickAudio) {
            if (canPlay) {
                currentMusic.setCurrentPosition(MediaService.mMediaPlayer.getCurrentPosition());
                currentMusic.setDuration(MediaService.mMediaPlayer.getDuration());
            } else {
                currentMusic.setCurrentPosition(0);
                currentMusic.setDuration(0);
            }
        } else {

//            Log.i("死掉了------------",String.valueOf((int)detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying()));
            if(canPlay && detailPlayer != null){
                currentMusic.setCurrentPosition((int)detailPlayer.getCurrentPlayer().getCurrentPositionWhenPlaying());
                currentMusic.setDuration((int)detailPlayer.getCurrentPlayer().getDuration());

            }else{
                currentMusic.setCurrentPosition(0);
                currentMusic.setDuration(0);
            }

        }


            GSYVideoManager.releaseAllVideos();
            detailPlayer.setVideoAllCallBack(null);
            if (orientationUtils != null){
                orientationUtils.releaseListener();
            }



        FloatMusicUtil.saveStatus(currentMusic);
        SharePreferencesUtils.saveFloatStatus(Constant.floatFlag);
        EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.APP_OPEN_FLOAT, currentMusic));
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {

        if (detailPlayer.isIfCurrentIsFullscreen()) {
            detailPlayer.onBackFullscreen();

            return;
        }
        //释放所有
        detailPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }

    @Override
    public void setStudyProgressSuccess() {

    }
    /**
     * 设置状态栏颜色
     *
     * @param activity
     */
    @SuppressLint("ObsoleteSdkInt")
    public static void setStatusBarColor(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            //状态栏改变颜色。
            window.setStatusBarColor(color);
        }
    }

    //=====================================服务相关===================================================
    //进入页面启动服务，必须携带音频资源开启
    private void initService() {
        MusicList.saveCurrentMusicData(coursePlayerBean);
        currentMusic = MusicList.getMusic(position);
        if (null == MediaService.mMediaPlayer) {
            initAudioService(currentMusic);
        } else {
            if (StringUtil.isNullString(from)) {
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NEXT_PREPARE, currentMusic));
            } else {
                currentMusic = fromMusic;
            }
        }
        if (null != fromMusic) {//悬浮窗进入
            position = fromMusic.getPosition();//很重要 带值进来更新位置
            if (fromMusic.isAudio()) {
                //展示音频页面
                isPlay = !fromMusic.isPlaying();

                detailPlayer.getCurrentPlayer().setSeekOnStart(fromMusic.getCurrentPosition());

                currentMusic = fromMusic;
                showFromAudioPage();

            } else {
                //默认是视频页面，不做处理
//                EventBusUtils.sendEvent(new EventBusEvent<Object>(EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE, EventBusUtils.EventCode.SERVICE_NOTIFICATION_CLOSE));
            }
            seek = fromMusic.getCurrentPosition();
        }
    }

    private void initAudioService(Music currentMusic) {
        Intent MediaServiceIntent = new Intent(this, MediaService.class);
        MediaServiceIntent.putExtra(Constant.service_data, currentMusic);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(MediaServiceIntent);
        } else {
            startService(MediaServiceIntent);
        }
    }
}
